#ifndef HISTOGRAM1D_H
#define HISTOGRAM1D_H

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

class Histogram1D
{
public:
    Histogram1D()
    {
        // Prepare arguments for 1D histogram
        histSize[0] = 256;
        hranges[0] = 0.0;
        hranges[1] = 255.0;
        ranges[0] = hranges;
        channels[0] = 0; // by default, we look at channel 0
    }
    ~Histogram1D();
    // Computes the 1D histogram and returns an image of it.
    cv::Mat getHistogramImage(const cv::Mat &image);
    // Computes the 1D histogram.
    cv::MatND getHistogram(const cv::Mat &image);

    /**
     * @brief stretch 拉伸图像的灰度直方图以增强图像的对比度
     * @param image 输入图像，必须是8 bits 灰度图像
     * @param percent 直方图两侧各舍弃百分之 percent 的点，将剩下的拉伸到 0 - 255
     * @return 返回一个新的图像
     */
    cv::Mat stretch(const cv::Mat &image, double percent);
    cv::Mat stretch(const cv::Mat &image, double percent1, double percent2);
    //直方图正规化，将图像图像最亮的地方线性拉伸到 255，最暗的地方线性拉伸到 0
    cv::Mat normalize(const cv::Mat &image);
    void colorReduce(cv::Mat &image, int div=64) ;
private:
    int histSize[1]; // number of bins
    float hranges[2]; // min and max pixel value
    const float* ranges[1];
    int channels[1]; // only 1 channel used here

};

class ColorHistogram
{
public:
    ColorHistogram()
    {
        // Prepare arguments for a color histogram
        histSize[0] = histSize[1] = histSize[2] = 256;
        hranges[0] = 0.0; // BRG range
        hranges[1] = 255.0;
        ranges[0] = hranges; // all channels have the same range
        ranges[1] = hranges;
        ranges[2] = hranges;
        channels[0] = 0; // the three channels
        channels[1] = 1;
        channels[2] = 2;
    }
    cv::MatND getHistogram(const cv::Mat &image) ;
    cv::SparseMat getSparseHistogram(const cv::Mat &image) ;
    void colorReduce(cv::Mat &image, int div=64) ;
private:
    int histSize[3];
    float hranges[2];
    const float* ranges[3];
    int channels[3];
};

#endif // HISTOGRAM1D_H
